// pages/orderDetail/orderDetail.js

//获取app实例
const app = getApp();
var utils = require("../../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: { },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      status: options.status
    })
    wx.showLoading({ title: "加载中..." });
    wx.request({
      url: app.baseURL + 'Integral&a=record_detail',
      data: {
        user_id: app.globalData.userId,
        id: options.orderId
      },
      success: (res) => {
        if (res.statusCode == 200) {
          res.data.info.addTime = utils.formatTime(res.data.info.add_time*1000); 
          this.setData({
            info: res.data.info
          })
          wx.hideLoading();
        }
      }
    })
  },
  //去支付
  goPay(){
    let result = JSON.stringify(this.data.info);
    wx.redirectTo({
      url: '/pages/orderPay/orderPay?result=' + result + '&integral=1'
    })
  },

  // 确认收货
  confirmReceipt(e) {
    let order_id = e.currentTarget.dataset.order_id;
    wx.showModal({
      title: '确认收到货了吗？',
      content: '为保障您的售后权益,请收到货确认无误后,再确认收货哦！',
      confirmText: "确认收货",
      confirmColor: "#f00",
      success: (res) => {
        if (res.confirm) {
          //加载中提示
          wx.showLoading({ title: '加载中' });
          wx.request({
            url: app.baseURL + 'integral&a=getted',
            data: {
              user_id: app.globalData.userId,
              order_id: order_id
            },
            success: (res) => {
              if (res.statusCode == 200) {
                if (res.data.status == 1) {
                  //收货成功
                  wx.hideLoading();
                  //前往结果页面
                  wx.reLaunch({ url: "/pages/commentResult/commentResult?title=收货成功&tip=您已成功收货" });
                } else if (res.data.status == 2) {
                  //收货失败
                  wx.hideLoading();
                  wx.showToast({ title: '操作失败，请重试', icon: 'none' })
                }
              }
            },
            fail: (res) => {
              wx.hideLoading();
              wx.showToast({ title: '网络错误', icon: 'none' })
            }
          })
        }
      }
    })
  },

})